package com.vue.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.vue.constant.MessageConstant;
import com.vue.context.BaseContext;
import com.vue.dto.NewPasswordDTO;
import com.vue.dto.LoginDTO;
import com.vue.dto.StudentPageQueryDTO;
import com.vue.entity.Student;
import com.vue.exception.AccountInfoErrorException;
import com.vue.exception.AccountNotFoundException;
import com.vue.exception.PasswordErrorException;
import com.vue.mapper.StudentMapper;
import com.vue.result.PageResult;
import com.vue.service.StudentService;
import com.vue.dto.StudentDTO;
import com.vue.vo.UserInfoVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.DigestUtils;

import java.util.List;

@Service
public class StudentServiceImpl implements StudentService {

    @Autowired
    private StudentMapper studentMapper;

    public Student login(LoginDTO loginDTO) {
        String username = loginDTO.getUsername();
        String password = loginDTO.getPassword();
        UserInfoVO userInfoVO = new UserInfoVO();
        userInfoVO.setUsername(username);

        //1、根据用户名查询数据库中的数据
        Student student = studentMapper.getStudent(userInfoVO);

        //2、处理各种异常情况（用户名不存在、密码不对、账号被锁定）
        if (student == null) {
            //账号不存在
            throw new AccountNotFoundException(MessageConstant.ACCOUNT_NOT_FOUND);
        }

        //密码比对
        //对前端传过来的明文密码进行MD5加密处理
        password = DigestUtils.md5DigestAsHex(password.getBytes());
        if (!password.equals(student.getPassword())) {
            //密码错误
            throw new PasswordErrorException(MessageConstant.PASSWORD_ERROR);
        }

        //3、返回实体对象
        return student;
    }

    /**
     * 学生分页查询
     * @param studentPageQueryDTO
     * @return
     */
    public PageResult studentPageQuery(StudentPageQueryDTO studentPageQueryDTO) {
        PageHelper.startPage(studentPageQueryDTO.getPageNum(),studentPageQueryDTO.getPageSize());
        Page<Student> page = studentMapper.studentPageQuery(studentPageQueryDTO);

        long total = page.getTotal();
        List<Student> records = page.getResult();
        return new PageResult(total,records);
    }

    /**
     * 获取学生信息
     * @param userInfoDTO
     * @return
     */
    public Student getUserInfo(UserInfoVO userInfoDTO) {
        Long id = BaseContext.getCurrentId();
        userInfoDTO.setId(id);
        return studentMapper.getStudent(userInfoDTO);
    }

    /**
     * 学生找回密码
     * @param newPasswordDTO
     */
    public void forgetPassword(NewPasswordDTO newPasswordDTO) {
        String username = newPasswordDTO.getUsername();
        String phone = newPasswordDTO.getPhone();
        String email = newPasswordDTO.getEmail();
        String password = newPasswordDTO.getNewPassword();
        String rePassword = newPasswordDTO.getRePassword();
        UserInfoVO userInfoVO = new UserInfoVO();
        userInfoVO.setUsername(username);
        Student student = studentMapper.getStudent(userInfoVO);
        if (student == null) {
            throw new AccountNotFoundException(MessageConstant.ACCOUNT_NOT_FOUND);
        }
        if (!student.getPhone().equals(phone)){
            throw new AccountInfoErrorException(MessageConstant.PHONE_NOT_FOUND);
        }
        if (!student.getEmail().equals(email)){
            throw new AccountInfoErrorException(MessageConstant.EMIAL_NOT_FOUND);
        }
        if (!password.equals(rePassword) || password.length()<5 || password.length()>15){
            throw new PasswordErrorException(MessageConstant.NEW_PASSWORD_ERROR);
        }
        student.setPassword(DigestUtils.md5DigestAsHex(password.getBytes()));
        studentMapper.update(student);
    }

    /**
     * 根据id删除学生
     * @param id
     */
    public void deleteById(Long id) {
        studentMapper.deleteById(id);
    }


    /**
     * 添加学生
     * @param studentDTO
     */
    public void add(StudentDTO studentDTO) {
        Student student = new Student();
        BeanUtils.copyProperties(studentDTO, student);
        studentMapper.insert(student);
    }

    /**
     * 修改学生信息
     * @param studentDTO
     */
    public void update(StudentDTO studentDTO) {
        Student student = new Student();
        BeanUtils.copyProperties(studentDTO, student);
        studentMapper.update(student);
    }

    /**
     * 学生修改密码
     * @param newPwd
     */
    public void updatePwd(String newPwd) {
        Student student = new Student();
        Long id = BaseContext.getCurrentId();
        student.setId(id);
        student.setPassword(DigestUtils.md5DigestAsHex(newPwd.getBytes()));
        studentMapper.update(student);
    }
}
